/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.standard.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.basicdata.standard.entity.BasicdataCheckStandardMain;
import com.jeeplus.modules.basicdata.standard.service.BasicdataCheckStandardMainService;
import com.jeeplus.modules.basicdata.standard.service.CheckRepeatedEntryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * 设备点检标准Controller
 * @author 王志鹏
 * @version 2020-09-25
 */
@RestController
@RequestMapping(value = "/basicdata/standard/basicdataCheckStandardMain")
public class BasicdataCheckStandardMainController extends BaseController {
	@Autowired
	private CheckRepeatedEntryService checkRepeatedEntryService;
	@Autowired
	private BasicdataCheckStandardMainService basicdataCheckStandardMainService;

	@ModelAttribute
	public BasicdataCheckStandardMain get() {
		BasicdataCheckStandardMain entity = new BasicdataCheckStandardMain();
		return entity;
	}

	/**
	 * 设备点检标准列表数据
	 */
	@RequiresPermissions("basicdata:standard:basicdataCheckStandardMain:list")
	@GetMapping("list")
	public AjaxJson list(BasicdataCheckStandardMain basicdataCheckStandardMain, HttpServletRequest request, HttpServletResponse response) {
		Page<BasicdataCheckStandardMain> page = basicdataCheckStandardMainService.findPage(new Page<BasicdataCheckStandardMain>(request, response), basicdataCheckStandardMain);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取设备点检标准数据
	 */
	@RequiresPermissions(value={"basicdata:standard:basicdataCheckStandardMain:view","basicdata:standard:basicdataCheckStandardMain:add","basicdata:standard:basicdataCheckStandardMain:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(@RequestParam(required=true) String id) {
		BasicdataCheckStandardMain basicdataCheckStandardMain = basicdataCheckStandardMainService.get(id);
		return AjaxJson.success().put("basicdataCheckStandardMain", basicdataCheckStandardMain);
	}

	/**
	 * 保存设备点检标准
	 */
	@RequiresPermissions(value={"basicdata:standard:basicdataCheckStandardMain:add","basicdata:standard:basicdataCheckStandardMain:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(BasicdataCheckStandardMain basicdataCheckStandardMain, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(basicdataCheckStandardMain);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		basicdataCheckStandardMainService.save(basicdataCheckStandardMain);//保存
		return AjaxJson.success("保存设备点检标准成功");
	}

//	@RequiresPermissions(value={"basicdata:standard:basicCheckMainForm:add","basicdata:standard:basicCheckMainForm:edit"},logical=Logical.OR)
	@GetMapping("CheckGet")
	public AjaxJson CheckGet(String id, String partdeviceId){
		checkRepeatedEntryService.CheckGet(id, partdeviceId);
		return AjaxJson.success("保存成功");
	}

	/**
	 * 批量删除设备点检标准
	 */
	@RequiresPermissions("basicdata:standard:basicdataCheckStandardMain:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			basicdataCheckStandardMainService.delete(basicdataCheckStandardMainService.get(id));
		}
		return AjaxJson.success("删除设备点检标准成功");
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("basicdata:standard:basicdataCheckStandardMain:export")
    @GetMapping("export")
    public AjaxJson exportFile(BasicdataCheckStandardMain basicdataCheckStandardMain, HttpServletRequest request, HttpServletResponse response) {
		try {
            String fileName = "设备点检标准"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<BasicdataCheckStandardMain> page = basicdataCheckStandardMainService.findPage(new Page<BasicdataCheckStandardMain>(request, response, -1), basicdataCheckStandardMain);
    		new ExportExcel("设备点检标准", BasicdataCheckStandardMain.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error("导出设备点检标准记录失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("basicdata:standard:basicdataCheckStandardMain:import")
    @PostMapping("import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<BasicdataCheckStandardMain> list = ei.getDataList(BasicdataCheckStandardMain.class);
			for (BasicdataCheckStandardMain basicdataCheckStandardMain : list){
				try{
					basicdataCheckStandardMainService.save(basicdataCheckStandardMain);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条设备点检标准记录。");
			}
			return AjaxJson.success( "已成功导入 "+successNum+" 条设备点检标准记录"+failureMsg);
		} catch (Exception e) {
			return AjaxJson.error("导入设备点检标准失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 下载导入设备点检标准数据模板
	 */
	@RequiresPermissions("basicdata:standard:basicdataCheckStandardMain:import")
    @GetMapping("import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		try {
            String fileName = "设备点检标准数据导入模板.xlsx";
    		List<BasicdataCheckStandardMain> list = Lists.newArrayList();
    		new ExportExcel("设备点检标准数据", BasicdataCheckStandardMain.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error( "导入模板下载失败！失败信息："+e.getMessage());
		}
    }


}